window.onload = function() {
  var Serializer = require('/lib/serializer');
  var Config = require('/lib/config');
  var host = window.location.origin.replace(/^http/, 'ws');
 // for client side websocket api, it is built-in for web browser
 // see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
 // for detail
  var socket = new WebSocket(host); 
  var game;

  socket.onopen = function(){
    console.log(">>connected");
    game = new Game(socket, Serializer);
  };
  socket.onmessage = function(message){
    var data = message.data;
    var msg = Serializer.deserialize(data);
    if (msg.t) {
      switch (msg.t) {
        case Serializer.MSG_NEW_PLAYER:
          game.createPlayer(msg.i);
          break;
        case Serializer.MSG_PING:
          socket.send(data);
          break;
        case Serializer.MSG_PLAYER_POSITION:
          game.updatePlayer(msg);
          break;
        case Serializer.MSG_NEW_SHELL:
          game.addShell(msg);
          break;
        case Serializer.MSG_GONE_PLAYER:
          game.removePlayer(msg.i);
          break;
        case Serializer.MSG_RESTART_GAME:
          console.log("restarting game after " + msg.ta + " s.");
            setTimeout(function() {
              game.restart();
            }, msg.ta + 1000);
          break;
        default:
          console.log("unknown message:" + msg);
      }
    }
  };
  socket.onclose = function(){
    console.log("server says: oh, bye");
  };
};
